package com.technology.education.ui.common.goods

import android.widget.ImageView
import androidx.core.os.bundleOf
import com.angcyo.dsladapter.toDslItemList
import com.technology.education.R
import com.technology.education.base.BaseRecyclerTopBarFragment
import com.technology.education.data.model.GoodsList
import com.technology.education.ext.loadImage
import com.technology.education.ext.navigation
import com.technology.education.ext.setPageItems
import me.hgj.jetpackmvvm.ext.parseState

/**
 *@author:create by BZY
 *Date: 2022/3/21 10
 * 积分兑换商品列表
 */
class GoodsListFragment : BaseRecyclerTopBarFragment<GoodsListViewModel>() {
    override fun initViews() {
        setTitle("积分商城")
    }

    override fun lazyLoadData() {
        loading()
        mViewModel.getData()
    }

    override fun createObserver() {
        mViewModel.result.observe(this) {
            parseState(it, onSuccess = { data ->
                renderAdapter {
                    setPageItems(data.list.toDslItemList(R.layout.item_goods_list) {
                        itemBindOverride =
                            { itemHolder, _, _, _ ->
                                itemData?.let { goods ->
                                    if (goods is GoodsList) {
                                        itemHolder.tv(R.id.tv_name)?.text = goods.name
                                        itemHolder.tv(R.id.tv_info)?.text = goods.description
                                        itemHolder.tv(R.id.tv_score)?.text = "${goods.price}积分"
                                        itemHolder.v<ImageView>(R.id.iv_goods)?.apply {
                                            loadImage(goods.coverImage, this)
                                        }
                                        itemHolder.click(R.id.btn_convert) { view ->
                                            navigation(
                                                view,
                                                R.id.action_goodsListFragment_to_confirmGoodOrderFragment,
                                                bundleOf("info" to goods)
                                            )
                                        }
                                    }
                                }
                            }
                    }, mViewModel.pageIndex)
                }
            }, onError = {
                loadError()
            })
        }
    }
}